//
//  LaunchViewController.m
//  YYZB
//
//  Created by 顾悦 on 16/4/18.
//
//

#import "LaunchViewController.h"

#import "UserInfoController.h"

#import "UserWebService.h"

#import "LoadingView.h"
#import "ThemeColor.h"

#import "MainViewController.h"
#import "CommitInfoViewController.h"
#import "LogViewController.h"

#import "AppDelegate.h"

@interface LaunchViewController ()

@property (nonatomic, strong) UserWebService *userWebService;

@end

@implementation LaunchViewController

- (UserWebService *)userWebService {
    if (!_userWebService) {
        _userWebService = UserWebService.new;
    }
    
    return _userWebService;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = [ThemeColor lightLightGrayBGColor];
    [self.userWebService login:[UserInfoController userNo] password:[UserInfoController password] iphoneID:[[UIDevice currentDevice].identifierForVendor UUIDString] completionHanlder:^(NSArray *items, NSError *error) {
        [[NSUserDefaults standardUserDefaults]setObject:[items valueForKey:@"HelperStatus"] forKey:@"status"];
        [[NSUserDefaults standardUserDefaults]setObject:[items valueForKey:@"HelperType"] forKey:@"type"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        dispatch_async(dispatch_get_main_queue(), ^{
            if (error) {
                [LoadingView showToast:[error localizedDescription]];
                LogViewController * logvc = [[LogViewController alloc]init];
                UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:logvc];
                [UIApplication sharedApplication].keyWindow.rootViewController = nav;
            } else {
                if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"status"]integerValue]==0||[[[NSUserDefaults standardUserDefaults]objectForKey:@"status"]integerValue]==3) {
                    CommitInfoViewController * vc = [[CommitInfoViewController alloc]init];
                    UINavigationController *navConroller = [[UINavigationController alloc] initWithRootViewController:vc];
                    [UIApplication sharedApplication].keyWindow.rootViewController = navConroller;
                }
                else if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"status"]integerValue]==2){
                    UIStoryboard * story = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                    MainViewController * vc = [story instantiateViewControllerWithIdentifier:@"MainViewController"];
                    vc.selectedIndex = 1;
                    vc.delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
                    [UIApplication sharedApplication].keyWindow.rootViewController = vc;
                } else {
                    UIStoryboard * story = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                    MainViewController * vc = [story instantiateViewControllerWithIdentifier:@"MainViewController"];
                    vc.selectedIndex = 2;
                    vc.delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
                    [UIApplication sharedApplication].keyWindow.rootViewController = vc;}
            }
        });
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
